#include <cmath>
#include <QDebug>
#include <QFontMetrics>
#include "element.h"


DrawCorner::DrawCorner(void)
    : m_start(0, 0),
      m_mid(0, 0),
      m_end(0, 0)
{
    m_drawType = CORNER;
    m_state = Element::DS_IDLE;
}

DrawCorner::~DrawCorner(void)
{
}
void DrawCorner::setStartPoint(QPoint start)
{
    m_start = start;
    m_mid = start;
    m_end = start;
    m_state = Element::DS_STATE1;
}

inline void DrawCorner::setSecondPoint(QPoint second)
{
    m_mid = second;
}

void DrawCorner::setEndPoint(QPoint end)
{
    m_end = end;
    computeAngle();
    QPoint centre = (m_mid + m_end) / 2;
    m_textBox.moveTo(centre);
    updateText();
    m_state = Element::DS_FINISH;
}
void DrawCorner::build(QPoint start, QPoint second, QPoint end)
{
    setStartPoint(start);
    setSecondPoint(second);
    setEndPoint(end);
}

int	DrawCorner::mouseMove(QPoint point)
{
    if( m_state == Element::DS_STATE1 )
    {
        setSecondPoint(point);
        return 1;
    }
    else if( m_state == Element::DS_STATE2 )
    {
        m_end = point;
        return 1;
    }
    return 0;
}

int	DrawCorner::mouseLButtonPress(QPoint point)
{
    if( m_state == Element::DS_IDLE )
    {
        setStartPoint(point);
        return 1;
    }
    return 0;
}

int DrawCorner::mouseLButtonRelease(QPoint point)
{
    if( m_state == Element::DS_STATE1 )
    {
        setSecondPoint(point);
        if( m_mid == m_start )
        {
            m_state = Element::DS_ERROR;
        }
        else
        {
            m_state = Element::DS_STATE2;
        }
        return 1;
    }
    else if( m_state == Element::DS_STATE2 )
    {
        setEndPoint(point);
        if( m_mid == m_end )
        {
            m_state = Element::DS_ERROR;
        }
        return 1;
    }
    return 0;
}

void DrawCorner::draw( QPainter &painter )
{
    if( m_state != Element::DS_IDLE )
    {
        painter.save();
        painter.setPen(m_linePen);
        if( m_state == Element::DS_STATE1 )
        {
            painter.drawLine(m_start, m_mid);
        }
        else if( m_state == Element::DS_STATE2 )
        {
            painter.drawLine(m_start, m_mid);
            painter.drawLine(m_mid, m_end);
        }
        else if( m_state == Element::DS_FINISH )
        {
            painter.drawLine(m_start, m_mid);
            painter.drawLine(m_mid, m_end);
            painter.setFont(m_textFont);
            painter.drawText(m_textBox, m_text);
        }
        painter.restore();
    }
}

void DrawCorner::computeAngle()
{
    double y1 = m_start.y() - m_mid.y();
    double x1 = m_start.x() - m_mid.x();
    double y2 = m_end.y() - m_mid.y();
    double x2 = m_end.x() - m_mid.x();
    double y3 = m_start.y() - m_end.y();
    double x3 = m_start.x() - m_end.x();
    double b = hypot(y1, x1);
    double c = hypot(y2, x2);
    double a = hypot(y3, x3);
    double theta = acos( ( b * b + c * c - a * a  )/ ( 2 * b * c ));
    m_angle = theta * 180 / 3.14;
}

void DrawCorner::updateText(void)
{
    m_text = QString::fromUtf8(u8"theta = %1°").arg(m_angle, 1, 'f', 1);

    QFontMetrics fm(m_textFont);
    int textWidth = fm.width(m_text);
    int textHeight = fm.height();
    m_textBox.setWidth(textWidth);
    m_textBox.setHeight(textHeight);
}

void DrawCorner::computeBox()
{

}
